package com.smartao.base.selectitem;

import java.util.List;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.smartao.base.module.Module;
import com.smartao.base.selecttype.Selecttype;
import com.smartao.base.selecttype.SelecttypeService;
import com.smartao.core.mvc.BaseAction;
import com.smartao.core.mvc.ResultStatus;

@Controller
@Scope("prototype")
@Results({ @Result(name = "success", location="list.jsp"),
		@Result(name = "listForUpdate", location="listForUpdate.jsp"),
		@Result(name = "view", location="view.jsp")})
public class SelectitemAction extends BaseAction{

	private static final long serialVersionUID = -5046460152122972714L;

	@Autowired
	private SelectitemService selectitemService;
	
	@Autowired
	private SelecttypeService selecttypeService;
	
	private Module module;
	
	private List<Selectitem> selectitems;
	
	private Selectitem selectitem;
	
	private Selectitem parent;
	
	private Long parentId;
	
	private Selecttype selecttype;
	
	private Long selecttypeId;
	
	@Override
	public String execute(){
		return SUCCESS;
	}
	
	public String listForUpdate(){
		// 有父选项；
		if(parentId!=null){
			parent = selectitemService.findOne(parentId);
			selecttype = parent.getSelecttype();
			selectitems = selectitemService.findByParent(parentId);
		}
		// 没有父选项；
		else{
			selectitems = selectitemService.findBySelecttype(selecttypeId);
			if(selecttype==null){
				selecttype = selecttypeService.findOne(selecttypeId);
			}
		}
		return "listForUpdate";
	}
	
	public String update(){
		for(Selectitem item : selectitems){
			// 忽略为null的新建对象；
			if(item!=null){
				selecttypeId = item.getSelecttype().getId();
				selectitemService.save(item);
			}
		}
		resultStatus = ResultStatus.SUCCESS;
		return listForUpdate();
	}
	
	public String delete(){
		if(selecttypeId==null){
			selectitem = selectitemService.findOne(id);
			selecttypeId = selectitem.getSelecttype().getId();
		}
		// TODO: 删除选择项还要考虑什么？
		selectitemService.delete(id);
		resultStatus = ResultStatus.SUCCESS;
		return listForUpdate();
	}

	public Module getModule() {
		return module;
	}

	public void setModule(Module module) {
		this.module = module;
	}

	public List<Selectitem> getSelectitems() {
		return selectitems;
	}

	public void setSelectitems(List<Selectitem> selectitems) {
		this.selectitems = selectitems;
	}

	public Selectitem getSelectitem() {
		return selectitem;
	}

	public void setSelectitem(Selectitem selectitem) {
		this.selectitem = selectitem;
	}

	public Selecttype getSelecttype() {
		return selecttype;
	}

	public void setSelecttype(Selecttype selecttype) {
		this.selecttype = selecttype;
	}

	public Long getSelecttypeId() {
		return selecttypeId;
	}

	public void setSelecttypeId(Long selecttypeId) {
		this.selecttypeId = selecttypeId;
	}

	public Selectitem getParent() {
		return parent;
	}

	public void setParent(Selectitem parent) {
		this.parent = parent;
	}

	public Long getParentId() {
		return parentId;
	}

	public void setParentId(Long parentId) {
		this.parentId = parentId;
	}
	
}
